package net.simo.servicebook.controller;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.inject.Produces;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import net.simo.servicebook.model.Person;
import net.simo.servicebook.service.ClientRegistration;
import java.io.Serializable;

@Named
@ConversationScoped
public class ClientController implements Serializable {

	private static final long serialVersionUID = 9171095097049669404L;

	@Inject
	private FacesContext facesContext;

	@Inject
	private ClientRegistration clientRegistration;

	private Person newPerson;

	@Produces
	@Named
	public Person getNewPerson() {
		return newPerson;
	}

	public void setNewPerson(Person newPerson) {
		this.newPerson = newPerson;
	}

	public void register() throws Exception {
		clientRegistration.register(newPerson);
		facesContext.addMessage(null, new FacesMessage(
				FacesMessage.SEVERITY_INFO, "Registered!",
				"Registration successful"));
		 initNewPerson();
	}

	public void editPerson() {
		clientRegistration.editPerson(newPerson);
		initNewPerson();
	}

	public void initPerson(Person person) {
		newPerson = new Person();
		newPerson = person;
	}

	public void deletePerson(Person person) {
		clientRegistration.deletePerson(person);
	}
	
	public void cancelPerson() {
		newPerson = null;
	}

	@PostConstruct
	public void initNewPerson() {
		newPerson = new Person();
	}
}
